package lyl.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.Duration;

/**
 * 日期&时间工具类
 * 
 * @author Liu Yulong
 */
public class DateTimeUtil {

    private static final int              MAJOR_VERSION    = 1;
    private static final int              MINOR_VERSION    = 0;
    private static final int              REVISION_VERSION = 1;
    private static final String           showFormat       = "yyyy-MM-dd HH:mm:ss";
    private static final String           storeFormat      = "yyyyMMddHHmmssSSS";
    private static final String           timeZoneFormat   = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    private static final SimpleDateFormat showFormater     = new SimpleDateFormat(showFormat);
    private static final SimpleDateFormat storeFormater    = new SimpleDateFormat(storeFormat);
    private static final SimpleDateFormat timeZoneFormater = new SimpleDateFormat(timeZoneFormat);
    private static final SimpleDateFormat formatter1       = new SimpleDateFormat(
                                                                   "yyyy年MM月dd日 HH时mm分ss秒");
    private static final SimpleDateFormat formatter2       = new SimpleDateFormat("yyyy年MM月dd日");
    private static final SimpleDateFormat formatter3       = new SimpleDateFormat(
                                                                   "yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat formatter4       = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat formatter5       = new SimpleDateFormat("yyyy/MM/dd");
    private static final SimpleDateFormat formatter6       = new SimpleDateFormat("MM-dd HH:mm");

    public DateTimeUtil() {}

    public static String getNow() {
        return storeFormater.format(new Date());
    }

    public static String getCurrentDate() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
    }

    public static Date currentDate() {
        return new Date(System.currentTimeMillis());
    }

    public static String getCurrentDate(String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date());
    }

    public static String getRelativeDate(int days) {
        Calendar c = Calendar.getInstance();
        c.set(5, c.get(5) + days);
        StringBuffer sb = new StringBuffer(17);
        sb.append(c.get(1));
        int tmp[] = { c.get(2) + 1, c.get(5), c.get(11), c.get(12), c.get(13), c.get(14) };
        for (int i = 0; i < tmp.length - 1; i++)
            sb.append(tmp[i] >= 10 ? "" : "0").append(tmp[i]);

        if (tmp[tmp.length - 1] < 10)
            sb.append("0");
        if (tmp[tmp.length - 1] < 100)
            sb.append("0");
        sb.append(tmp[tmp.length - 1]);
        return sb.toString();
    }

    public static String getNow(String string) {
        return (new SimpleDateFormat(string)).format(new Date());
    }

    public static String getDisplayTime(long time) {
        return showFormater.format(new Date(time));
    }

    public static String getDisplayTime(long time, String string) {
        return (new SimpleDateFormat(string)).format(new Date(time));
    }

    public static String getShowFormat(String time) {
        try {
            if (time == null || time.equals(""))
                time = showFormater.format(new Date());
            else
                time = showFormater.format(storeFormater.parse(time));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static long compare(String t1, String t2) {
        return Long.valueOf(t1).longValue() - Long.valueOf(t2).longValue();
    }

    public static int getYear(String time) {
        return Integer.valueOf(time.substring(0, 4)).intValue();
    }

    public static int getMonth(String time) {
        return Integer.valueOf(time.substring(4, 6)).intValue();
    }

    public static int getDate(String time) {
        return Integer.valueOf(time.substring(6, 8)).intValue();
    }

    public static int getHour(String time) {
        return Integer.valueOf(time.substring(8, 10)).intValue();
    }

    public static int getMinute(String time) {
        return Integer.valueOf(time.substring(10, 12)).intValue();
    }

    public static int getSecond(String time) {
        return Integer.valueOf(time.substring(12, 14)).intValue();
    }

    public static int getMilliSencond(String time) {
        return Integer.valueOf(time.substring(14, 17)).intValue();
    }

    public static long getTimeStamp(String time) throws ParseException {
        return storeFormater.parse(time).getTime();
    }

    public static long getZoneTime(String time) throws ParseException {
        return timeZoneFormater.parse(time).getTime();
    }

    public static String formatDate1(Date myDate) {
        return formatter1.format(myDate);
    }

    public static String formatDate2(Date myDate) {
        return formatter2.format(myDate);
    }

    public static String formatDate3(Date myDate) {
        return formatter3.format(myDate);
    }

    public static String formatDate4(Date myDate) {
        return formatter4.format(myDate);
    }

    public static String formatDate5(Date myDate) {
        return formatter5.format(myDate);
    }

    public static String formatDate6(Date myDate) {
        return formatter6.format(myDate);
    }

    public static String formatZoneTime(Date date) {
        return timeZoneFormater.format(date);
    }

    public static long getLongTime(String time) {
        try {
            return showFormater.parse(time).getTime();
        }
        catch (ParseException ex) {
            return 0L;
        }
    }

    public static String getVersion() {
        return "1.0.1";
    }

    public static int getMajor() {
        return 1;
    }

    public static int getMinor() {
        return 0;
    }

    public static int getRevision() {
        return 1;
    }

    public static void main(String[] args) {
        try {
            // System.out.println(DateTimeUtil.getZoneTime("2001-07-04T12:08:56.235Z"));

            Duration duration = javax.xml.datatype.DatatypeFactory.newInstance().newDuration(
                    "PT10M");
            // long interval = duration.getDays()*24*3600 +
            // duration.getHours()*3600 + duration.getMinutes()*60 +
            // duration.getSeconds();
            Date date = new Date(DateTimeUtil.getZoneTime("2001-07-04T12:08:56.235Z"));
            duration.addTo(date);

            System.out.println((new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")).parse("2001-07-04T12:08:56"));

        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (DatatypeConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
